<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Fashion extends MY_Controller{

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct()
    {
        parent::__construct();
		
		//Google Analytics ID
		$this->data['ga_id'] = 14;		
		
		//Load model
		//$this->load->model('fashion_model');
				
		//Load Fashion Script
		$this->data['js'][] = "fashionScript.js";
		//$this->data['css'][]   = (!ONIDEV ? 'defaultStyle2.min.css':'defaultStyle2.css');
		$this->data['css'][] = "fashionStyle.css";
		
		 //Load Library
  		$this->load->library('pagination');

		//Router Category
		$this->data['category'] = array(
			'magazine' => array('id' => 'Fashion Magazine','title' => 'Fashion Magazine'),
			'women' => array('id' => 'Fashion Women','title' => 'Fashion Women'),
			'men' => array('id' => 'Fashion Men','title' => 'Fashion Men'),
			'event' => array('id' => 'Event','title' => 'Event')
		);	
		
		//Set SEO Tag
		$this->data['seo_title'] = 'แฟชัน แฟชันเกาหลี แฟชันผู้หญิง สำหรับคนอินเทรนด์ วัยรุ่น แหล่งรวมเสื้อผ้าแฟชัน | You2Play.com';
		$this->data['seo_description'] = 'แฟชัน แฟชันเกาหลี แฟชันผู้หญิง สำหรับคนอินเทรนด์ วัยรุ่น แหล่งรวมเสื้อผ้าแฟชัน';
		$this->data['seo_keyword'] = 'แฟชันเกาหลี, แฟชันผู้หญิง, วัยรุ่น, เสื้อผ้า';							
    }
	
	/*Home*/	 
	public function index()
	{
		if($this->input->post('ajax')) $this->ajax_fashion_pager('Update',TRUE);

		//Load Javascript
		$this->data['js'][] = "jquery.multipleelements.cycle.min.js";

		//Load Module
		$this->data['module'] = $this->module('fashion');

		//Load Top Sub Menu
		$this->data['top_sub_menu'] = $this->load->view($this->router->class."/top_sub_menu",'', TRUE);

		//Fashion Hilight
		if ( !$this->data['dataFashionHilight'] = $this->memcache->get('dataFashionHilight')):
			$this->data['dataFashionHilight'] = $this->db->select('fashion_id,views,fashion_name,fashion_brief,create_date')
			->order_by('fashion_publish_date DESC,fashion_id DESC')
			->get_where('fashion',"is_main_hilight = '1' AND is_publish = '1' AND (fashion_publish_date IS NULL OR fashion_publish_date < NOW())",5)
			->result();
			foreach($this->data['dataFashionHilight'] as $value):
				$value->image_count = $this->db->from('fashion_image')->where(array('fashion_id' => $value->fashion_id))->count_all_results();
				$value->image = $this->db->order_by('fashion_image_order','ASC')->get_where('fashion_image',array('fashion_id' => $value->fashion_id),1)->row();
			endforeach;
			//Cache Set
			$this->memcache->set('dataFashionHilight', $this->data['dataFashionHilight'], $this->memcache_time);			
		endif;
		
		//Fashion Update
		if(!intval($this->input->get('p'))):
			if ( !$this->data['dataFashionUpdate'] = $this->memcache->get('dataFashionUpdate')):
				$this->data['dataFashionUpdate'] = $this->ajax_fashion_pager('Update');
				//Cache Set
				$this->memcache->set('dataFashionUpdate', $this->data['dataFashionUpdate'], $this->memcache_time);		
			endif;
		else:
			$this->data['dataFashionUpdate'] = $this->ajax_fashion_pager('Update');
		endif;	

		$this->data['content'] = $this->router->class."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);
	}
	
	public function magazine(){$this->category();}
	public function women(){$this->category();}
	public function men(){$this->category();}
	public function event(){$this->category();}
	private function category()
	{
		if($this->input->post('ajax')) $this->ajax_fashion_pager('Category',TRUE);

		//Load Module
		$this->data['module'] = $this->module('fashion');
				
		//Load Top Sub Menu
		$this->data['top_sub_menu'] = $this->load->view($this->router->class."/top_sub_menu",$this->data, TRUE);
		
		/*News Type Hilight*/
		$this->data['dataTypeHilight'] = $this->db->select('fashion_id,views,fashion_name,fashion_brief,create_date')
		->order_by("fashion_id DESC")
		->get_where('fashion',"fashion_category = '".$this->data['category'][$this->router->method]['id']."' AND is_type_hilight = '1' AND is_publish = '1' AND (fashion_publish_date IS NULL OR fashion_publish_date < NOW())",5)->result();
		foreach($this->data['dataTypeHilight'] as $value):
			$value->image_count = $this->db->from('fashion_image')->where(array('fashion_id' => $value->fashion_id))->count_all_results();
			$value->image = $this->db->order_by('fashion_image_order','ASC')->get_where('fashion_image',array('fashion_id' => $value->fashion_id),1)->row();
		endforeach;	
				
		//News Category
		$this->data['dataResult'] = $this->ajax_fashion_pager('Category');
		
		$this->data['content'] = $this->router->class."/category";
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);			
	}
	
	private function ajax_fashion_pager($mode,$ajax = FALSE)
	{
		switch($mode):
			case 'Update':
				$per_page = 32;
				$dataLastHilight = $this->db->select('fashion_id')
				->order_by('fashion_id','DESC')
				->get_where('fashion',"is_main_hilight = '1' AND is_publish = '1' AND (fashion_publish_date IS NULL OR fashion_publish_date < NOW())",5)
				->result();
				$and_where_not_in = '';
				foreach($dataLastHilight as $value):
					$and_where_not_in.= $value->fashion_id.',';
				endforeach;
				if(!empty($and_where_not_in)) $and_where_not_in = " AND fashion_id NOT IN (".rtrim($and_where_not_in,',').")";
				$searchCondition = "is_publish = '1'".$and_where_not_in;
				$order_by = "fashion_id DESC";
			break;
			case 'Category':
				$per_page = 32;
				$dataLastHilight = $this->db->select('fashion_id')
				->order_by('fashion_id','DESC')
				->get_where('fashion',"fashion_category = '".$this->data['category'][$this->router->method]['id']."' AND is_type_hilight = '1' AND is_publish = '1' AND (fashion_publish_date IS NULL OR fashion_publish_date < NOW())",5)
				->result();
				$and_where_not_in = '';
				foreach($dataLastHilight as $value):
					$and_where_not_in.= $value->fashion_id.',';
				endforeach;				
				if(!empty($and_where_not_in)) $and_where_not_in = " AND fashion_id NOT IN (".rtrim($and_where_not_in,',').")";				
				$searchCondition = "fashion_category = '".$this->data['category'][$this->router->method]['id']."' AND is_publish = '1' AND (fashion_publish_date IS NULL OR fashion_publish_date < NOW())".$and_where_not_in;
				$order_by = "fashion_id DESC";				
			break;
		endswitch;
		/*Pager*/		
		$config['base_url'] = current_url().'?'.query_string(array('p'));
		$config['total_rows'] = $this->db->from('fashion')
		->where($searchCondition)	
		->count_all_results();
		$config['per_page'] = $per_page;
		$config['num_links'] = 5;
		$config['first_link'] = '&laquo;';
		$config['first_tag_open'] = '<span class="first">';
		$config['first_tag_close'] = '</span>';				
		$config['last_link'] = '&raquo;';
		$config['last_tag_open'] = '<span class="last">';
		$config['last_tag_close'] = '</span>';		
		$config['next_tag_open'] = '<span class="prev">';
		$config['next_tag_close'] = '</span>';	
		$config['prev_tag_open'] = '<span class="next">';
		$config['prev_tag_close'] = '</span>';			
		$config['cur_tag_open'] = '<span class="num_tag current">';
		$config['cur_tag_close'] = '</span>';
		$config['num_tag_open'] = '<span class="num_tag">';
		$config['num_tag_close'] = '</span>';				
		$config['page_query_string'] = TRUE;
		$config['query_string_segment'] = 'p';
		$this->pagination->initialize($config);
		$this->data['pager'] = $this->pagination->create_links();
		$this->data['dataFashion'] = $this->db
		->order_by($order_by)			
		->get_where('fashion',$searchCondition,$config['per_page'],intval($this->input->get('p')))
		->result();
		
		foreach($this->data['dataFashion'] as $value):
			$value->image_count = $this->db->from('fashion_image')->where(array('fashion_id' => $value->fashion_id))->count_all_results();
			$value->image = $this->db->order_by('fashion_image_order','ASC')->get_where('fashion_image',array('fashion_id' => $value->fashion_id),1)->row();
		endforeach;
		
		if($ajax):
			echo $this->load->view($this->router->class."/ajax_fashion_pager", $this->data, TRUE);
			exit;
		else:
			return $this->load->view($this->router->class."/ajax_fashion_pager", $this->data, TRUE);		
		endif;		
	}
	
	/*Archives*/
	public function archives($id = NULL,$seo_text = NULL)
	{	
		if($this->input->post('ajax')) $this->ajax_fashion_image_pager($id,TRUE);
		
		$is_exist = $this->db->from('fashion')->where(array('fashion_id' => $id,'is_publish' => '1'))->count_all_results();
		if(empty($is_exist)) $this->page_missing();
	
		//Rediect if no seo text
		if(empty($seo_text)):
			$dataResult = $this->db->get_where('fashion',array('fashion_id' => $id),1)->row();
			redirect($fashion_url.'archives/'.intval($id).'-'.seo_url($dataResult->fashion_name), 'location', 301);
		endif;			
		
		//Load ColorBox
		$this->data['js'][] = "jquery.colorbox-min.js";	
		$this->data['css'][] = "colorbox.css";	

		//UPDATE Song (View)
		$this->db->set('views', 'views + 1', FALSE);
		$this->db->where(array('fashion_id' => $id));
		$this->db->update('fashion');
		
		//Keep Stat View
		$datetime = date('Y-m-d');
		$is_exist = $this->db->from('fashion_click_stat')->where(array('fashion_id' => $id,'update_date' => $datetime))->count_all_results();
		if(!empty($is_exist)):
			$this->db->set('fashion_click_views', 'fashion_click_views + 1', FALSE);
			$this->db->where(array('fashion_id' => $id,'update_date' => $datetime));
			$this->db->update('fashion_click_stat');
		else:
			$data = array(
				'fashion_id' => $id,
				'fashion_click_views' => 1,
				'update_date' => $datetime
			);
			$this->db->insert('fashion_click_stat',$data);
		endif;
		
		//Load Module
		$this->data['module'] = $this->module('fashion');
		$this->data['module_facebook_plugins_share'] = $this->module_facebook_plugins_share();
		$this->data['module_facebook_plugins_comment'] = $this->module_facebook_plugins_comment();	
		
		$this->data['dataResults'] = $this->db->select('fashion_id,views,fashion_category,fashion_name,fashion_description,create_date')
		->get_where('fashion as t',"fashion_id = '".$id."' AND is_publish = '1'",1)
		->row();
		$this->data['dataResults']->image = $this->db->order_by('fashion_image_order','ASC')->get_where('fashion_image',array('fashion_id' => $this->data['dataResults']->fashion_id),1)->row();		
		
		//Set SEO Tag
		$this->data['seo_title'] = $this->data['dataResults']->fashion_name.' | แฟชัน You2Play.com';	
		$this->data['fb_title'] = $this->data['seo_title'];
		if(!empty($this->data['dataResults']->image)):
			exec_image(array(
				'folder' => 'fashion',
				'dimension' => '398x298',
				'file_name' => @$this->data['dataResults']->image->fashion_file_name.'.'.@$this->data['dataResults']->image->fashion_file_ext,
				'alt' => ''.$this->data['dataResults']->fashion_name.'',	
				'title' => ''.$this->data['dataResults']->fashion_name.''	
			));
			$this->data['fb_image'] = image_asset_url('../uploads/fashion/images/398x298/'.$this->data['dataResults']->image->fashion_file_name.'.'.$this->data['dataResults']->image->fashion_file_ext);
		endif;
		$this->data['fb_type'] = 'article';
		
		//GA Events
		$this->data['ga_event'] = 'CI-'.$id.'-'.seo_url($this->data['dataResults']->fashion_name);				
		
		$this->data['dataFashionImage'] = $this->ajax_fashion_image_pager($id);
		
		//Load Top Sub Menu
		$this->data['top_sub_menu'] = $this->load->view($this->router->class."/top_sub_menu",'', TRUE);
		
		$this->data['content'] = $this->router->class."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);			
	}
	
	private function ajax_fashion_image_pager($id,$ajax = FALSE)
	{
		$searchCondition = "fashion_id = '".$id."'";
		$order_by = "fashion_image_order ASC,fashion_image_id ASC";
		/*Pager*/		
/*		$config['base_url'] = current_url().'?'.query_string(array('p'));
		$config['total_rows'] = $this->db->from('fashion_image')
		->where($searchCondition)
		->count_all_results();
		$config['per_page'] = 40;
		$config['num_links'] = 5;
		$config['first_link'] = '&laquo;';
		$config['first_tag_open'] = '<span class="first">';
		$config['first_tag_close'] = '</span>';				
		$config['last_link'] = '&raquo;';
		$config['last_tag_open'] = '<span class="last">';
		$config['last_tag_close'] = '</span>';		
		$config['next_tag_open'] = '<span class="prev">';
		$config['next_tag_close'] = '</span>';	
		$config['prev_tag_open'] = '<span class="next">';
		$config['prev_tag_close'] = '</span>';			
		$config['cur_tag_open'] = '<span class="num_tag current">';
		$config['cur_tag_close'] = '</span>';
		$config['num_tag_open'] = '<span class="num_tag">';
		$config['num_tag_close'] = '</span>';				
		$config['page_query_string'] = TRUE;
		$this->pagination->initialize($config);
		$this->data['pager'] = $this->pagination->create_links();
		$this->data['dataFashionImage'] = $this->db
		->order_by($order_by)		
		->get_where('fashion_image',$searchCondition,$config['per_page'],intval($this->input->get('p')))
		->result();*/

		$this->data['dataFashionImage'] = $this->db
		->order_by($order_by)		
		->get_where('fashion_image',$searchCondition)
		->result();		
		
		//Find prev/next fashion_id
		$this->data['dataFashionPrev'] = $this->db->select('fashion_id,fashion_name')
		->order_by('fashion_id','DESC')
		->get_where('fashion',"fashion_id < ".$id."",1)
		->row();
		$this->data['dataFashionNext'] = $this->db->select('fashion_id,fashion_name')
		->order_by('fashion_id','ASC')
		->get_where('fashion',"fashion_id > ".$id."",1)
		->row();
		
		if($ajax):
			echo $this->load->view($this->router->class."/ajax_fashion_image_pager", $this->data, TRUE);
			exit;
		else:
			return $this->load->view($this->router->class."/ajax_fashion_image_pager", $this->data, TRUE);		
		endif;		
	}	
}


/* End of file video.php */
/* Location: ./application/controllers/video.php */